#include"UDPserver.hpp"
#include<time.h>

void Usage(string proc)
{
    cout<<"use"<<proc<<" "<<"port"<<endl;
    exit(-1);
}

string GetTime()    //获取时间
{
    string value;
    time_t t=time(NULL);
    
    //时间戳转正常时间
    struct tm* tm_info=localtime(&t);
    value+=std::to_string(tm_info->tm_year+1900)+"/"+
    std::to_string(tm_info->tm_mon+1)+"/"+
    std::to_string(tm_info->tm_mday)+" | "+
    std::to_string(tm_info->tm_hour)+":"+
    std::to_string(tm_info->tm_min)+":"+
    std::to_string(tm_info->tm_sec);
    // std::cout<<value<<std::endl;
    
    return value;
}

std::string Handler(std::string f)    
{
    string prefix("客户端消息");
    prefix+="["+GetTime()+"]"+":";

    string ClientInfo=prefix+f;
    // std::cout<<ClientInfo<<std::endl;    //不做打印，临时调试
    cout<<ClientInfo<<endl<<endl;
    return ClientInfo;
}

int main(int argc,char* argv[])
{
    // cout<<argc<<endl;
    if(argc!=2)
        Usage(argv[0]);
    else
        defaultport=std::stoi(argv[1]);

    Server S;

    S.Init();
    S.Run(Handler);
    
    return 0;
}

